#!/bin/bash
# vim: set ft=sh

set -e -x

if [ -z "$BLOB_NAME" ]; then
  echo "must specify \$BLOB_NAME" >&2
  exit 1
fi

if [ -z "$BLOB_DESTINATION" ]; then
  echo "must specify \$BLOB_DESTINATION" >&2
  exit 1
fi

if [ -z "$BLOB_VERSION" ] && [ -z "$BLOB_VERSION_FILE" ] ; then
  echo "must specify \$BLOB_VERSION or \$BLOB_VERSION_FILE" >&2
  exit 1
fi

blob_dir=$PWD/blob-dir

if [ -n $BLOB_VERSION_FILE ]; then
  BLOB_VERSION=`cat ${blob_dir}/${BLOB_VERSION_FILE}`
fi

git clone concourse bumped-concourse

set +x
echo "$BOSH_PRIVATE_CONFIG" > bumped-concourse/config/private.yml
set -x

cd bumped-concourse/

# work-around Go BOSH CLI trying to rename blobs downloaded into ~/.root/tmp
# into release dir, which is invalid cross-device link
export HOME=$PWD

git config --global user.email "ci@localhost"
git config --global user.name "CI Bot"

bosh -n add-blob $blob_dir/*.tar.gz ${BLOB_DESTINATION}/${BLOB_NAME}.tar.gz
bosh -n upload-blobs

git add -A
git commit -m "update ${BLOB_NAME} blob to ${BLOB_VERSION}"
